package cn.fl.ass.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.ass.service.IAssChnBankAccService;
import cn.fl.ass.vo.AssChnBankAccVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSONObject;
import fl.ass.facade.condition.AssChnBankAccQC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Description: 拖车渠道银行账户信息表 控制层
 */
@Controller
@RequestMapping("/ass/assChnBankAcc")
public class AssChnBankAccController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AssChnBankAccController.class);

	/**
	 * AssChnBankAcc 服务层BEAN
	 */
	@Autowired
	private IAssChnBankAccService assChnBankAccService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        String chnId = request.getParameter("chnId");//拖车渠道ID
		ModelAndView view = new ModelAndView("ass/assChnBankAcc/assChnBankAccList");
        view.addObject("chnId", chnId);
		return view;
	}

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListDeatilPage.do", method = { RequestMethod.GET})
    public ModelAndView toListDeatilPage(HttpServletRequest request, HttpServletResponse response) {
        String chnId = request.getParameter("chnId");//拖车渠道ID
        ModelAndView view = new ModelAndView("ass/assChnBankAcc/listDeatilPage");
        view.addObject("chnId", chnId);
        return view;
    }

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("ass/assChnBankAcc/modify");
		view.addObject("id", id);
		return view;
	}

    /**
     * 到新增/修改拖车渠道银行账号信息
     */
    @RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET })
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String chnId = request.getParameter("chnId");
        ModelAndView view = new ModelAndView("/ass/assChnBankAcc/assChnBankAccAdd");
        AssChnBankAccVO assChnBankAccVO = null;
        List<KeyValueVO> accTypCdList = this.assChnBankAccService.searchCdList(DictionaryConstant.CODE_GRP_CST_NP_ACC_TYP_CD);
        List<KeyValueVO> bkTypCdList = this.assChnBankAccService.searchCdList(DictionaryConstant.CODE_GRP_CST_NP_BK_CD);
        view.addObject("accTypCdList", accTypCdList);
        view.addObject("bkTypCdList", bkTypCdList);
        view.addObject("chnId", chnId);//传递渠道ID
        view.addObject("bid", bid);//传递

        return view;
    }

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String channelId = request.getParameter("channelId");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		AssChnBankAccQC qc = new AssChnBankAccQC();
		if (StringUtils.isNotEmpty(channelId)) {
            qc.setChannelId(Long.valueOf(channelId));
		}
        qc.setIsDel(BaseConstant.IS_YESNO_NO);
		page.setCondition(qc);

		Pager pager = this.assChnBankAccService.searchListPage(page);
        List<AssChnBankAccVO> list = (List<AssChnBankAccVO>) pager.getResultList();
		ListPageVO<AssChnBankAccVO> listPage = new ListPageVO<AssChnBankAccVO>(list, pager.getTotalCount());
		return listPage;
	}

    /**
     * 启用 禁用 拖车渠道银行账户
     */
    @RequestMapping(value = "/enable.do", method = { RequestMethod.POST })
    @ResponseBody
    public Object enable(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        String isValid = request.getParameter("isValid");//是否有效
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if(StringUtils.isEmpty(id)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.newLogger("启用或禁用拖车渠道银行账户,主键参数为空,UserNm:" + CurrentThreadContext.getCurrentUserName()).info();
            return result;
        }
        if(StringUtils.isEmpty(isValid)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_PARAM_IS_BAD);
            BaseLogUtils.newLogger("启用或禁用拖车渠道银行账户, 是否有效参数为空,UserNm:" + CurrentThreadContext.getCurrentUserName()).info();
            return result;
        }

        try {
            boolean success = this.assChnBankAccService.enable(Long.valueOf(id), Integer.valueOf(isValid));
            if(!success){
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_ENABLE_FAIL);
                BaseLogUtils.newLogger("启用或禁用拖车渠道银行账户失败").setKeys("id:" + id).info();
            }
            BaseLogUtils.newLogger("启用或禁用拖车渠道银行账户成功").setKeys("id:" + id).info();
        }catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.newLogger("启用或禁用拖车渠道失败").setThrowable(ex).setKeys("id:" + id).info();
        }catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.newLogger("启用或禁用拖车渠道失败,系统错误").setThrowable(ex).setKeys("id:" + id).info();
        }
        return result;
    }

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		try {
			this.assChnBankAccService.removeById(Long.valueOf(id));
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		
		try {
			AssChnBankAccVO vo = this.assChnBankAccService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
			}
			result.setData(vo);
			BaseLogUtils.newLogger("assChnBankAccController 获取数据").setKeys("id:" + id).info();
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}
		return result;
	}

    /**
     * 根据渠道ID获取启用的银行账户
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getByChannelId.do", method = { RequestMethod.GET})
    @ResponseBody
    public Object getByChannelId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String channelId = request.getParameter("channelId");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if(StringUtils.isEmpty(channelId)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            return result;
        }

        try {
            AssChnBankAccVO vo = this.assChnBankAccService.getByChannelId(Long.valueOf(channelId));
            if(null == vo){
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
            }
            result.setData(vo);
            BaseLogUtils.newLogger("根据渠道ID获取启用的银行账户").setKeys("channelId:" + channelId).info();
        }catch(BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
        }catch(Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
        }
        return result;
    }

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String bkCd = request.getParameter("bkCd");
        String accNm = request.getParameter("accNm");
        String accNo = request.getParameter("accNo");
        String accOrgNm = request.getParameter("accOrgNm");
        String bkNo = request.getParameter("bkNo");
        String accTypCd = request.getParameter("accTypCd");
        String channelId = request.getParameter("channelId");
        String bkRem = request.getParameter("bkRem");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        if(StringUtils.isEmpty(channelId)){
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo("拖车渠道ID不能为空");
            return result;
        }

		AssChnBankAccVO entity = null;
		try {
			entity = new AssChnBankAccVO();
            entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            entity.setCrtTm(new Date());
            entity.setBkCd(bkCd);
            entity.setAccNm(accNm);
            entity.setAccNo(accNo);
            entity.setAccOrgNm(accOrgNm);
            entity.setBkNo(bkNo);
            entity.setAccTypCd(accTypCd);
            entity.setBkRem(bkRem);
            entity.setChannelId(Long.valueOf(channelId));
			Long id = this.assChnBankAccService.add(entity);
			result.setData(id);
			BaseLogUtils.newLogger("AssChnBankAccController 新增数据").setKeys("id:" + id, "entity:" + JSONObject.toJSONString(entity)).info();
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String bid = request.getParameter("bid");
        String bkCd = request.getParameter("bkCd");
        String accNm = request.getParameter("accNm");
        String accNo = request.getParameter("accNo");
        String accOrgNm = request.getParameter("accOrgNm");
        String bkNo = request.getParameter("bkNo");
        String accTypCd = request.getParameter("accTypCd");
        String channelId = request.getParameter("channelId");
        String bkRem = request.getParameter("bkRem");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(bid)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.newLogger("AssChnBankAccController 修改数据,主键不存在").info();
			return result;
		}
		AssChnBankAccVO entity = null;
		try {
			entity = new AssChnBankAccVO();
            entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
            entity.setMdfTm(new Date());
			entity.setId(Long.valueOf(bid)); entity.setBkCd(bkCd);
            entity.setAccNm(accNm);
            entity.setAccNo(accNo);
            entity.setAccOrgNm(accOrgNm);
            entity.setBkNo(bkNo);
            entity.setAccTypCd(accTypCd);
            entity.setBkRem(bkRem);
            entity.setChannelId(Long.valueOf(channelId));
			boolean success = this.assChnBankAccService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.newLogger("AssChnBankAccController 修改失败").setKeys("entity:" + JSONObject.toJSONString(entity)).info();
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
		}
		return result;
	}
}

